package com.ayu.a.practice02;

import java.util.Arrays;

/**
 * @Author 徐林玉
 * @Description StrStrPrac
 * @Date 2022/11/15 13:47
 * @Version 1.0
 */
public class StrStrPrac {

    public static void main(String[] args) {


        String haystack = "hello";

        String needle = "ll";


        int i = strStr(haystack, needle);

        System.out.println("i = " + i);

    }

    public static int strStr(String haystack,String needle){


        char[] c1 = haystack.toCharArray();

        char[] c2 = needle.toCharArray();


        for (int i = 0; i < c1.length; i++) {

            if(c1[i]==c2[0]){

                if(c2.length<=c1.length-i){

                    char[] c3 = Arrays.copyOfRange(c1, i, i+c2.length);

                    String s1 = String.valueOf(c3);

                    if(s1.equalsIgnoreCase(needle)){

                        return i;
                    }

                }

            }

        }

        return -1;

    }
}
